$(function () {
    $("#jqGrid").jqGrid({
        url: '../activity/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', key: true, hidden: true, align: "center"},
            {label: '活动名称', name: 'title', index: 'title', width: 130, align: "center"},
            {
                label: '列表封面图',
                name: 'listCoverUrl',
                index: 'list_cover_url',
                width: 80,
                align: "center",
                formatter: function (value) {
                    return transImg(value);
                }
            },
            {label: '活动举办城市', name: 'activityCity', index: 'activity_city', width: 80, align: "center"},
            {label: '活动具体地点', name: 'activityPlace', index: 'activity_place', width: 80, align: "center"},
            {
                label: '活动开始时间', name: 'activityStartTime', index: 'activity_start_time', width: 110, align: "center",
                formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd hh:mm:ss');
                }
            },
            {
                label: '活动结束时间',
                name: 'activityEndTime',
                index: 'activity_end_time',
                width: 110,
                align: "center",
                formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd hh:mm:ss');
                }
            },
            // {label: '地点的纬度', name: 'latitude', index: 'latitude', width: 80, align: "center"},
            // {label: '地点的经度', name: 'longitude', index: 'longitude', width: 80, align: "center"},
            // {label: '活动详情', name: 'contentDesc', index: 'content_desc', width: 80, align: "center"},
            {label: '标签', name: 'tags', index: 'tags', width: 80, align: "center"},
            {
                label: '是否结束',
                name: 'isFinish',
                index: 'is_finish',
                width: 50,
                align: "center",
                formatter: function (value) {
                    return value === 1 ?
                        '<span class="label label-success">是</span>' :
                        '<span class="label label-danger">否</span>';
                }
            },
            // {
            //     label: '添加时间',
            //     name: 'addTime',
            //     index: 'add_time',
            //     width: 80,
            //     align: "center",
            //     formatter: function (value) {
            //         return transDate(value, 'yyyy-MM-dd hh:mm:ss');
            //     }
            // },
            {label: '小程序链接', name: 'wxAppLink', index: 'wx_app_link', width: 180, align: "center"},
            // {label: '更新时间', name: 'updateTime', index: 'update_time', width: 80, align: "center"}
        ],
        viewrecords: true,
        height: 495,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
    $('#contentDesc').editable({
        inlineMode: false,
        alwaysBlank: true,
        height: '300px', //高度
        minHeight: '200px',
        language: "zh_cn",
        spellcheck: false,
        plainPaste: true,
        enableScript: false,
        imageButtons: ["floatImageLeft", "floatImageNone", "floatImageRight", "linkImage", "replaceImage", "removeImage"],
        allowedImageTypes: ["jpeg", "jpg", "png", "gif"],
        imageUploadURL: '../sys/oss/upload',
        imageUploadParams: {id: "edit"},
        imagesLoadURL: '../sys/oss/queryAll'
    })
});


let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        activity: {},
        ruleValidate: {
            title: [
                {required: true, message: '名称不能为空', trigger: 'blur'},
                {max: 50, message: '名称请输入最多50位'}
            ],
            brief: [
                {required: true, message: '活动简介不能为空', trigger: 'blur,change'}
            ],
            listCoverUrl: [
                {required: true, message: '封面图不能为空', trigger: 'blur,change'}
            ],
            activityCity: [
                {required: true, message: '举办城市不能为空', trigger: 'blur'},
                {max: 50, message: '举办城市请输入最多50位'}
            ],
            activityStartTime: [
                {required: true, message: '活动开始时间不能为空', trigger: 'blur,change', type: 'date'}
            ],
            activityEndTime: [
                {required: true, message: '活动结束时间不能为空', trigger: 'blur,change', type: 'date'}
            ],
            activityPlace: [
                {required: true, message: '具体地点不能为空', trigger: 'blur'},
                {max: 50, message: '具体地点请输入最多50位'}
            ],
            latitude: [
                {required: true, message: '地点的纬度不能为空', trigger: 'blur'},
                {max: 50, message: '纬度请输入最多50位'}
            ],
            longitude: [
                {required: true, message: '地点的经度不能为空', trigger: 'blur'},
                {max: 50, message: '经度请输入最多50位'}
            ],
            tags: [
                {required: true, message: '标签不能为空', trigger: 'blur'},
                {max: 150, message: '标签请输入最多150位'}
            ]

        },
        q: {
            name: ''
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.activity = {
                isFinish: 0,
                listCoverUrl: '',
                latitude: '0.000001',
                longitude: '0.000001'
            };
            $('#contentDesc').editable('setHTML', '');
        },
        update: function (event) {
            let id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            let url = vm.activity.id == null ? "../activity/save" : "../activity/update";
            vm.activity.contentDesc = $('#contentDesc').editable('getHTML');
            vm.activity.activityEndTime = new Date(+vm.activity.activityEndTime + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '');
            vm.activity.activityStartTime = new Date(+vm.activity.activityStartTime + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '');

            if (vm.activity.contentDesc === "" || vm.activity.contentDesc == null || vm.activity.contentDesc === undefined) {
                alert('活动详情不能为空');
                return;
            }

            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify(vm.activity),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            let ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: "../activity/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get("../activity/info/" + id, function (r) {
                vm.activity = r.activity;
                $('#contentDesc').editable('setHTML', vm.activity.contentDesc);
            });
        },
        reload: function (event) {
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        handleSuccessPicUrl: function (res, file) {
            vm.activity.listCoverUrl = file.response.url;
        },
        handleFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传 jpg 或 png 格式的图片。'
            });
        },
        handleMaxSize: function (file) {
            this.$Notice.warning({
                title: '超出文件大小限制',
                desc: '文件 ' + file.name + ' 太大，不能超过 2M。'
            });
        },
        eyeImagePicUrl: function () {
            var url = vm.activity.listCoverUrl;
            eyeImage(url);
        },
        activityRecord: function () {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            openWindow({
                title: '活动报名记录',
                type: 2,
                content: '../shop/activityrecord.html?activityId=' + id
            })
        }
    }
});